package start.spring.basic.util;

import java.util.List;

/**
 * ****************************************************************************
 * 分页查询的辅助类，需要“当前页currentPage”及“每页显示条数numPerPage”两个参数
 * 默认值分别为currentPage = 10，numPerPage = 1
 *
 * @author(作者)：xuyongyun	
 * @author(复审人)：	Administrator 2012-8-1
 * @version(版本)1.0
 * @since 1.0
 ******************************************************************************
 */
public class PageUtil <T> {
	
	/**
	 * 默认每页的查询数量
	 */
	private static final int NUMPERPAGE = 20;  
	
	/**
	 * 默认查询第几页
	 */
	private static final int CURRENTPAGE = 1;  
	
	/**
	 * 查询时是否需要查询数据总条数，默认需要查询，
	 * 此属性为了减少分页时数据库的查询压力，需要根据具体业务设置
	 * 设置为false时，需要自己传入totalRecords
	 */
	private static final boolean ISGETCOUNT = true;
	
	/**
	 * 每页数据量
	 */
	public int numPerPage;
	
	/**
	 * 总数据量
	 */
	public long totalRecords;
    
	/**
	 * 是否查询总数据量
	 */
	public boolean isQueryCount;
    
	/**
	 * 总页数
	 */
	public long totalPages;
    
	/**
	 * 当前查询页数
	 */
	public long currentPage;
    
	/**
	 * 上一页码
	 */
	public long prePage;  
    
	/**
	 * 下一页码
	 */
	public long nextPage;
    
	/**
	 * 是否有上一页
	 */
	public boolean hasPrePage;
    
	/**
	 * 是否有下一页
	 */
	public boolean hasNextPage; 
    
	/**
	 * 当前页首条数据记录序号
	 */
	public long startIndex;
    
	/**
	 * 当前页最后一条数据记录序号
	 */
	public long lastIndex;
    
	/**
	 * 当前页数据
	 */
	public List<T> resultList;
    
    public PageUtil() {
    	this.numPerPage = NUMPERPAGE;
    	this.currentPage = CURRENTPAGE;
    	this.isQueryCount = ISGETCOUNT;
	}

	public long getCurrentPage() {
    	return currentPage;
    }
    
    public void setCurrentPage(long currentPage) {
    	this.currentPage = (currentPage == 0 ? 1 : currentPage);
    }
    
    public int getNumPerPage() {
    	return numPerPage;
    }
    
    public void setNumPerPage(int numPerPage) {
    	this.numPerPage = (numPerPage == 0 ? 10 : numPerPage);
    }
    
    public long getPrePage() {
    	return prePage;
    }
    
    public void setPrePage(long prePage) {
    	this.prePage = (prePage < 1 ? 1 : prePage);
    }
    
    public long getNextPage() {
    	return nextPage;
    }
    
    public void setNextPage(long nextPage) {
    	this.nextPage = (nextPage > totalPages ? totalPages : nextPage);
    }
    
    public boolean getHasPrePage() {
    	this.hasPrePage = (currentPage == 1 ? false : true);
    	return hasPrePage;
    }
    
    public boolean getHasNextPage() {
    	this.hasNextPage = (currentPage == totalPages || totalPages == 0 ? false : true);
    	return hasNextPage;
    }
    
	public List<T> getResultList(){
    	return resultList;
    }
    
	public void setResultList(List<T> resultList) {
    	this.resultList = resultList;
    }
    
    public long getTotalPages() {
    	if (totalRecords % numPerPage == 0){
	    	this.totalPages = totalRecords / numPerPage;
	    }else{
	    	this.totalPages = totalRecords / numPerPage + 1;
	    }
    	return totalPages;
    }
    
    public long getTotalRecords() {
    	return totalRecords;
    }
    
    public void setTotalRecords(long totalRecords) {
    	this.totalRecords = totalRecords;
    }

	public boolean getIsQueryCount() {
		return isQueryCount;
	}

	public void setIsQueryCount(boolean isQueryCount) {
		this.isQueryCount = isQueryCount;
	}

	public long getStartIndex() {
    	this.startIndex = (currentPage - 1) * numPerPage;
    	return startIndex;
    }
    
    public long getLastIndex() {
    	if( totalRecords < numPerPage){
    		this.lastIndex = totalRecords;
    	}else if((totalRecords / numPerPage == 0) || (totalRecords / numPerPage != 0 && currentPage < getTotalPages())){
    		this.lastIndex = currentPage * numPerPage;
    	}else if(totalRecords / numPerPage != 0 && currentPage == getTotalPages()){
    		this.lastIndex = totalRecords;
    	}
    	return lastIndex;
    }
}
